﻿public class Solution394
{
    public string DecodeString(string s)
    {

        Stack<int> numStack = new Stack<int>();
        Stack<string> strStack = new Stack<string>();
        string curStr = null;

        int k = 0;
        for (int i = 0; i < s.Length; i++)
        {


            char ch = s[i];


            if (char.IsDigit(ch))//是数字
            {

                k = k * 10 + (ch - '0');


            }
            else
            {

                if (ch == '[')//左括号
                {
                    //把前面的存起来
                    strStack.Push(curStr);
                    //数字的结尾，存数字
                    numStack.Push(k);
                    k = 0;
                    curStr = "";//清空，重新累计


                }
                else if (ch == ']')
                {
                    string pre = strStack.Pop();
                    //当前括号遍历完，乘于重复次数
                    int reaptTimes = numStack.Pop();
                    for (int t = 0; t < reaptTimes; t++)
                    {
                        pre += curStr;
                    }


                    curStr = pre;

                }
                else
                {//字符，直接加入当前结果

                    curStr += ch;
                }






            }



        }



        return curStr;

    }
}